{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.18 \u4f20\u9012\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u7ed9C\u6269\u5c55\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5728Python\u4e2d\u6709\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\u4f46\u662f\u9700\u8981\u5c06\u5b83\u4f20\u7ed9\u8981\u4f7f\u7528\u8fd9\u4e2a\u6587\u4ef6\u7684C\u6269\u5c55\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u5c06\u4e00\u4e2a\u6587\u4ef6\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u578b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4f7f\u7528 PyFile_FromFd() \uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "PyObject *fobj;     /* File object (already obtained somehow) */\nint fd = PyObject_AsFileDescriptor(fobj);\nif (fd < 0) {\n   return NULL;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u901a\u8fc7\u8c03\u7528 fobj \u4e2d\u7684 fileno() \u65b9\u6cd5\u83b7\u5f97\u7684\u3002\n\u56e0\u6b64\uff0c\u4efb\u4f55\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u66b4\u9732\u7ed9\u4e00\u4e2a\u63cf\u8ff0\u5668\u7684\u5bf9\u8c61\u90fd\u9002\u7528\uff08\u6bd4\u5982\u6587\u4ef6\u3001\u5957\u63a5\u5b57\u7b49\uff09\u3002\n\u4e00\u65e6\u4f60\u6709\u4e86\u8fd9\u4e2a\u63cf\u8ff0\u5668\uff0c\u5b83\u5c31\u80fd\u88ab\u4f20\u9012\u7ed9\u591a\u4e2a\u4f4e\u7ea7\u7684\u53ef\u5904\u7406\u6587\u4ef6\u7684C\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u8f6c\u6362\u4e00\u4e2a\u6574\u578b\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u4e00\u4e2aPython\u5bf9\u8c61\uff0c\u9002\u7528\u4e0b\u9762\u7684 PyFile_FromFd() :"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "int fd;     /* Existing file descriptor (already open) */\nPyObject *fobj = PyFile_FromFd(fd, \"filename\",\"r\",-1,NULL,NULL,NULL,1);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "PyFile_FromFd() \u7684\u53c2\u6570\u5bf9\u5e94\u5185\u7f6e\u7684 open() \u51fd\u6570\u3002\nNULL\u8868\u793a\u7f16\u7801\u3001\u9519\u8bef\u548c\u6362\u884c\u53c2\u6570\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u5c06Python\u4e2d\u7684\u6587\u4ef6\u5bf9\u8c61\u4f20\u7ed9C\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u3002\n\u9996\u5148\uff0cPython\u901a\u8fc7 io \u6a21\u5757\u6267\u884c\u81ea\u5df1\u7684I/O\u7f13\u51b2\u3002\n\u5728\u4f20\u9012\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ed9C\u4e4b\u524d\uff0c\u4f60\u90fd\u8981\u9996\u5148\u5728\u76f8\u5e94\u6587\u4ef6\u5bf9\u8c61\u4e0a\u5237\u65b0I/O\u7f13\u51b2\u3002\n\u4e0d\u7136\u7684\u8bdd\uff0c\u4f60\u4f1a\u6253\u4e71\u6587\u4ef6\u7cfb\u7edf\u4e0a\u9762\u7684\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u6b21\uff0c\u4f60\u9700\u8981\u7279\u522b\u6ce8\u610f\u6587\u4ef6\u7684\u5f52\u5c5e\u8005\u4ee5\u53ca\u5173\u95ed\u6587\u4ef6\u7684\u804c\u8d23\u3002\n\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u4f20\u7ed9C\uff0c\u4f46\u662f\u5728Python\u4e2d\u8fd8\u5728\u88ab\u4f7f\u7528\u7740\uff0c\u4f60\u9700\u8981\u786e\u4fddC\u6ca1\u6709\u610f\u5916\u7684\u5173\u95ed\u5b83\u3002\n\u7c7b\u4f3c\u7684\uff0c\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2aPython\u6587\u4ef6\u5bf9\u8c61\uff0c\u4f60\u9700\u8981\u6e05\u695a\u8c01\u5e94\u8be5\u53bb\u5173\u95ed\u5b83\u3002\nPyFile_FromFd() \u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u88ab\u8bbe\u7f6e\u62101\uff0c\u7528\u6765\u6307\u51faPython\u5e94\u8be5\u5173\u95ed\u8fd9\u4e2a\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u4eceC\u6807\u51c6I/O\u5e93\u4e2d\u4f7f\u7528\u5982\u3000fdopen() \u51fd\u6570\u6765\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u5bf9\u8c61\u6bd4\u5982 FILE * \u5bf9\u8c61\uff0c\n\u4f60\u9700\u8981\u7279\u522b\u5c0f\u5fc3\u4e86\u3002\u8fd9\u6837\u505a\u4f1a\u5728I/O\u5806\u6808\u4e2d\u4ea7\u751f\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684I/O\u7f13\u51b2\u5c42\n\uff08\u4e00\u4e2a\u662f\u6765\u81eaPython\u7684 io \u6a21\u5757\uff0c\u53e6\u4e00\u4e2a\u6765\u81eaC\u7684 stdio \uff09\u3002\n\u50cfC\u4e2d\u7684 fclose() \u4f1a\u5173\u95edPython\u8981\u4f7f\u7528\u7684\u6587\u4ef6\u3002\n\u5982\u679c\u8ba9\u4f60\u9009\u7684\u8bdd\uff0c\u4f60\u5e94\u8be5\u4f1a\u9009\u62e9\u53bb\u6784\u5efa\u4e00\u4e2a\u6269\u5c55\u4ee3\u7801\u6765\u5904\u7406\u5e95\u5c42\u7684\u6574\u578b\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\n\u800c\u4e0d\u662f\u4f7f\u7528\u6765\u81ea<stdio.h>\u7684\u9ad8\u5c42\u62bd\u8c61\u529f\u80fd\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}